package com.springboot.film.controller;

import com.springboot.film.entity.Page;
import com.springboot.film.entity.looktime;
import com.springboot.film.service.looktimeService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class looktimeCtrl {

    @Autowired
    private looktimeService looktimeService;

    //分页显示全部
    @RequestMapping("/houtai/looktime")
    public String looktime(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = looktimeService.getCount();
        List<looktime> looktimeList=new ArrayList<looktime>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            looktimeList=looktimeService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            looktimeList=looktimeService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_looktime");
        model.addAttribute("show_left", "left_looktime");
        model.addAttribute("left", "left_y_looktime");
        model.addAttribute("mypage", "looktime");
        model.addAttribute("looktime", looktimeList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_looktime";
    }

    //分页显示全部搜索
    @RequestMapping("/houtai/searchlooktime")
    public String searchlooktime(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="b.name";
        if(types == 1) {
            keyword_new = "b.name";
        }else if (types == 2) {
            keyword_new = "c.name";
        }
        totalCount = looktimeService.getsearchCount(keyword_new,keyword);
        List<looktime> looktimeList=new ArrayList<looktime>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            looktimeList=looktimeService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            looktimeList=looktimeService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(looktimeList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_looktime");
            model.addAttribute("show_left", "left_looktime");
            model.addAttribute("left", "left_y_looktime");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("looktime", looktimeList);
            return "houtai_looktime";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/houtai/looktime?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/houtai/addlooktime")
    @ResponseBody
    public String addlooktime(looktime looktime) {
        looktimeService.houtaiadd(looktime);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/houtai/updatelooktime")
    public String updatelooktime(looktime looktime) {
        looktimeService.update(looktime);
        return "redirect:/houtai/looktime";
    }

    //删除
    @RequestMapping("/houtai/deletelooktime")
    public String deletelooktime(int userid) {
        looktimeService.delete(userid);
        return "redirect:/houtai/looktime";
    }

    //跳转到更改页面
    @RequestMapping("/houtai/to_updatelooktime")
    public String to_updatelooktime(Model model, int userid) {
        List<looktime> looktimeList=new ArrayList<looktime>();
        looktimeList = looktimeService.getUserone(userid);
        model.addAttribute("top", "top_looktime");
        model.addAttribute("show_left", "left_updatelooktime");
        model.addAttribute("left", "left_y_looktime");
        model.addAttribute("mypage", "looktime");
        model.addAttribute("looktime", looktimeList);
        return "houtai_updatelooktime";
    }

    //跳转到添加页面
    @RequestMapping("/houtai/to_addlooktime")
    public String to_addlooktime(Model model) {
        model.addAttribute("top", "top_looktime");
        model.addAttribute("show_left", "left_addlooktime");
        model.addAttribute("left", "left_y_looktime");
        model.addAttribute("mypage", "looktime");
        return "houtai_addlooktime";
    }

}
